package com.lmx.upload;

import com.lmx.bean.UploadUnit;
import com.lmx.cloud.FileUploadQueue;
import com.lmx.control.WlanManager;
import com.lmx.device.CollectManage;
import com.lmx.util.LogUtil;

import java.io.*;
import java.net.Socket;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

public class Client {
    private static String server_ip;
    private static int server_port;
    private static String device_name;
    private static Socket socket;
    private static FileInputStream fileInputStream;
    private static DataOutputStream dataOutputStream;
    private static File file;
    private static String file_name;

    public static void init() {
//        server_ip="localhost";
//        server_ip="192.168.4.1";
        server_ip = WlanManager.gatewayAddress.trim();
        System.out.println("server_ip");
        System.out.println("server_ip:" + server_ip);
        server_port = WlanManager.port;
        device_name = WlanManager.device_name;
    }

    public static void sendData(UploadUnit uploadUnit) {
        String name;
        try {
            socket = new Socket(server_ip, server_port);
        } catch (IOException e) {
            LogUtil.log_error("Cannot connect to gateway !");
            DeprecatedFileUploadQueue.add(uploadUnit);
            try {
                TimeUnit.SECONDS.sleep(10);
                return;
            } catch (InterruptedException interruptedException) {
                interruptedException.printStackTrace();
            }
            return;
        }


        try {

            file = uploadUnit.getFile();
            fileInputStream = new FileInputStream(file);
            dataOutputStream = new DataOutputStream(socket.getOutputStream());

//            //transfer file_name
//            file_name = device_name + "@" + fileUnit.getFile_name();
//            dataOutputStream.writeUTF(file_name);
//            dataOutputStream.flush();
//            //transfer file_name
//            dataOutputStream.writeLong(file.length());
//            dataOutputStream.flush();

            //transfer file
            byte[] bytes = new byte[1024];
            int length = 0;

            while ((length = fileInputStream.read(bytes, 0, bytes.length)) != -1) {
                dataOutputStream.write(bytes, 0, length);
                dataOutputStream.flush();
            }

            LogUtil.log_info("[+] Upload " + file.getName() + " to gateway successfully");

            name = file.getName();
            if (file.exists()) {
                Objects.requireNonNull(CollectManage.get("/superradra")).remove_files(name);
            }

            if (file.delete()) {
                System.out.println("success");
            } else {
                System.out.println("failed");
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            LogUtil.log_info("[+] Upload " + file.getName() + " failed");
            DeprecatedFileUploadQueue.add(uploadUnit);
        } catch (IOException e) {
            e.printStackTrace();
            LogUtil.log_info("[+] Upload " + file.getName() + " failed");
            DeprecatedFileUploadQueue.add(uploadUnit);
        } finally {
            try {
                fileInputStream.close();
                dataOutputStream.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
}
